﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace WindowsFormsApplication1
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnexp_Click(object sender, EventArgs e)
        {            
            Form nextW = new ExpOptions(this);
            nextW.ShowDialog(this);
        }

        private void btnIngresar_Click(object sender, EventArgs e)
        {
            String user = txtnom.Text;
            String psswd = txtpwd.Text;

            EmbeddedDatabase db = EmbeddedDatabase.Instance;

            SqlCeDataReader k = db.GetDataReaderForCustomQuery("SELECT * FROM Usuarios");
            bool allow = false;
            while (k.Read())
                if (k.GetString(0).Equals(user) && k.GetString(1).Equals(psswd))
                    allow = true;

            if (allow)
            {
                btnexp.Enabled = true;
                txtnom.Enabled = false;
                txtpwd.Enabled = false;
                grpLogin.Visible = false;
                grpLogout.Visible = true;
            }
            else
            {
                MessageBox.Show("El nombre de usuario o la contraseña son inválidos.", "Error al intentar ingresar");
                txtnom.Focus();
                txtnom.SelectAll();
            }
        }

        private void btnCerrarSesion_Click(object sender, EventArgs e)
        {
            txtnom.Text = "";
            txtpwd.Text = "";

            btnexp.Enabled = false;
            txtnom.Enabled = true;
            txtpwd.Enabled = true;
            grpLogin.Visible = true;
            grpLogout.Visible = false;
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }
    }
}
